﻿using EONFULLSIN2.Models.Dto.Menu;
using EONFULLSIN2.Models.Dto.User;
using EONFULLSIN2.Models.Entity.User;
using EONFULLSIN2.Services.DB;
using EONFULLSIN2.View;
using Newtonsoft.Json;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EONFULLSIN2.Common
{
    /// <summary>
    /// 全局应用信息
    /// </summary>
    public class AppInfo
    {
        /// <summary>
        /// 应用名称
        /// </summary>
        public static string AppactionName => ConfigurationManager.AppSettings["AppactionName"]?.ToString();

        /// <summary>
        /// 应用版本
        /// </summary>
        public static string Version => ConfigurationManager.AppSettings["Version"]?.ToString();

        /// <summary>
        /// 菜单列表
        /// </summary>
        public static ObservableCollection<MenuItemDto> MenuList => new ObservableCollection<MenuItemDto>()
        {
            new MenuItemDto{ Name = "首页", Code = nameof(MainView), Icon = MaterialDesignThemes.Wpf.PackIconKind.HomeOutline, },
            new MenuItemDto{ Name = "设备操作", Code = nameof(DeviceOperationView), Icon = MaterialDesignThemes.Wpf.PackIconKind.GestureDoubleTap, },
            new MenuItemDto{ Name = "数据采集", Code = nameof(DeviceManagerView),Icon = MaterialDesignThemes.Wpf.PackIconKind.LanConnect, },
            new MenuItemDto{ Name = "流程控制", Code = nameof(DeviceControlView),Icon = MaterialDesignThemes.Wpf.PackIconKind.Controller, },
            new MenuItemDto{ Name = "数据追溯", Code = nameof(DataHistoryView),Icon = MaterialDesignThemes.Wpf.PackIconKind.DatabaseSearchOutline, },
            new MenuItemDto{ Name = "系统管理", Code = nameof(SystemManagerView),Icon = MaterialDesignThemes.Wpf.PackIconKind.CogOutline, },
        };

        /// <summary>
        /// 当前用户
        /// </summary>
        public static User CurrentUser { get; set; }

        /// <summary>
        /// AppConfig配置
        /// </summary>
        public static AppConfig Config { get; set; }

        /// <summary>
        /// 初始化AppConfig
        /// </summary>
        public static void InitAppConfig() {
            if (File.Exists(AppConfig.SavePath)) {
                try {
                    var json = File.ReadAllText(AppConfig.SavePath);
                    Config = (string.IsNullOrEmpty(json) ? new AppConfig() : JsonConvert.DeserializeObject<AppConfig>(json)) ?? new AppConfig();
                } catch {
                    Config = new AppConfig();
                }
            } else {
                Config = new AppConfig();
            }
        }

        /// <summary>
        /// 保存AppConfig
        /// </summary>
        public static void SaveAppConfig() {
            var json = JsonConvert.SerializeObject(Config, Formatting.Indented);
            File.WriteAllText(AppConfig.SavePath, json);
        }

    }
}
